package controller

import (
	"github.com/gookit/validate"
	"kong-social-api/commons/base/controller"
	"kong-social-api/modules/common/sms/model/context"
	"kong-social-api/modules/common/sms/service"
)

type SMSController struct {
	controller.BaseController
}

var smsService = service.SMSService{}

// 发送短信
func (web *SMSController) SendPhoneCode() {
	// 获取短信上下文结构体
	phoneContext := context.SMSPhoneContext{}
	// 绑定参数
	web.BindJSON(&phoneContext)
	// 校验参数
	validation := validate.Struct(&phoneContext)
	if !validation.Validate() {
		web.FailWithValidatorData(validation)
		return
	}

	// 使用redis缓存来进行数据共享

	// 根据手机号发送短信
	code, flag, err := smsService.SendPhoneCode(phoneContext.Telephone)
	// 如果结果是true发送成功，false发送失败
	if flag {
		// 证明短信发送成功
		web.Ok(code)
	} else {
		web.FailData(err)
	}
}
